package hu.village.controller;

import hu.village.pojo.Account;
import hu.village.pojo.PageBean;
import hu.village.pojo.Result;
import hu.village.pojo.User;
import hu.village.service.UserService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.valves.rewrite.RewriteCond;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j

public class UserController {

    @Autowired
    private UserService userService;

    //发送注册验证码
    @GetMapping("village/register/email")
    public Result emailRegister(String email){
        log.info("发送注册验证码,邮箱{}",email);
        userService.sendRegisterEmail(email);
        return Result.success();
    }
    //用户注册
    @GetMapping("village/register")
    public Result Register(@RequestBody User user){
        log.info("用户注册,{}",user);
        String msg =userService.Register(user);
        if(msg.equals("注册成功"))
        {
            return Result.success();
        }else{
            return Result.error(msg);
        }
    }
    //用户登录
    @GetMapping("village/login")
    public Result login(@RequestBody User user){
        log.info("用户登录,{}",user);
        User u = userService.login(user);
        if(u == null){
            return Result.error("用户不存在");
        }
        return Result.success("登录成功");
    }

    //用户修改个人信息
    @GetMapping("village/user/update")
    public Result updateUser(@RequestBody User user){
        log.info("修改用户信息:{}",user);
        userService.updateUser(user);
        return Result.success();
    }

    //查看审核通过的账目信息
    @GetMapping("village/user/select")
    public Result selectAccount(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "5") Integer pageSize){
        log.info("查询审核通过账目");
        PageBean p  = userService.selectAccount(page,pageSize);
        return Result.success(p);
    }

    //举报账目,账目状态改为1
    @GetMapping("village/user/report")
    public Result reportAccount(String reportMan,Integer id){
        log.info("举报账目,举报人:{},举报账目id:{}",reportMan,id);
        userService.reportAccount(reportMan,id);
        return Result.success();
    }

    //查看举报申诉情况
    @GetMapping("village/user/selectReportAccount")
    public Result selectReportedAccount(@RequestBody Account account){
        log.info("查询申诉结果,账目:{}",account);
        Integer state = userService.selectReportAccount(account);
        if(state == 0){
            return Result.success("成功");
        } else if (state == 1) {
            return Result.success("审核中");
        }else{
            return Result.success("失败");
        }
    }
    //撤销举报
    @GetMapping("village/user/repel")
    public Result repelReport(@RequestBody Account account){
        log.info("撤销举报,账目:{}",account);
        userService.repelReport(account);
        return Result.success("撤销成功");
    }

    //申请看以往账目
    @GetMapping("village/user/selectHistoryAccount")
    public Result selectHistoryAccount(@RequestBody User user){
        log.info("申请查看过往账目,申请人:{}",user);
        userService.selectHistoryAccount(user);
        return Result.success("申请成功");
    }

}
